﻿using EntityModel;
using Microsoft.AspNetCore.Mvc;


namespace EGS.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        private readonly EGSDbContext db;
        public LoginController(EGSDbContext context)
        {
            db = context;
        }

        //public Result Login(string _Account, string _Password)
        //{
        //    Result result = new Result();
        //    EGS_Account? account = db.Account.Where(s => s.Account == _Account && s.Password == _Password).SingleOrDefault();
        //    result.Data = account;
        //    result.Name = "12345678@qq.com";
        //    result.Code = account == null ? 404 : 200;
        //    return result;
        //}

        public List<EGS_LoginInfo> GetAll()
        {
            return db.LoginInfo.Where(l => l.DelTime != null).OrderByDescending(l => l.ID).ToList();
        }


        [HttpGet]
        public List<EGS_LoginInfo> GetAll ([FromQuery] string Ip, string Name)
        {
            IQueryable<EGS_LoginInfo> query = db.LoginInfo;
            if(!string.IsNullOrEmpty(Ip))
            {
                query = query.Where(l => l.IP == Ip);
            }
            if (!string.IsNullOrEmpty(Name))
            {
                query = query.Where(l => l.Name == Name);
            }
            return query.ToList();
            //IQueryable<EGS_LoginInfo> loginInfo = db.LoginInfo;
            //if (!string.IsNullOrEmpty(Ip))
            //{
            //    loginInfo = loginInfo.Where(l => l.LoginInfo == loginInfo);
            //}
            //return loginInfo.ToList();
        }
        [HttpGet]
        public List<EGS_LoginInfo> GetBySelect([FromBody] string Ip,string Name)
        {
            List<EGS_LoginInfo> LoginInfo = new List<EGS_LoginInfo>();
            if(!string.IsNullOrEmpty(Ip))
            {
                LoginInfo = db.LoginInfo.Where(l => l.IP == Ip).ToList();
            }
            if(!string.IsNullOrEmpty(Name))
            {
                LoginInfo = db.LoginInfo.Where(l => l.Name == Name).ToList();
            }
            return LoginInfo;
        }

        /// <summary>
        /// 查
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public EGS_LoginInfo? GetEGS_LoginInfoById(Guid id)
        {
            EGS_LoginInfo? login= new EGS_LoginInfo();
            login = db.LoginInfo.Where(l => l.ID == id).FirstOrDefault();
            return login;
        }
        /// <summary>
        /// 模糊查询名字
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet("like/{name}")]
        public List<EGS_LoginInfo> GetEGS_LoginInfoLikeName(string name)
        {
            List<EGS_LoginInfo> login = new List<EGS_LoginInfo>();
            login = db.LoginInfo.Where(l => l.Name.Contains(name)).ToList();
            return login;
        }

        /// <summary>
        /// 增
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public Result AddLogin([FromBody] EGS_LoginInfo login)
        {
            Result result = new Result();
            db.LoginInfo.Add(login);
            result.Data = db.SaveChanges();
            return result;
        }
        /// <summary>
        /// 删
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete]
        public Result DelLogin(Guid Id)
        {
            Result? result = new Result();
            EGS_LoginInfo? loginInfo = new EGS_LoginInfo();
            loginInfo = db.LoginInfo.Where(l => l.ID == Id).FirstOrDefault();
            //int result = 0;
            if (loginInfo != null)
            {
                loginInfo.DelTime = DateTime.Now;
                db.LoginInfo.Update(loginInfo);
                result.Data = db.SaveChanges();
                //result.Code = 200;
                //result.Time = DateTime.Now;
                //result.Name = "张三";
            }
            return result;
        }

        /// <summary>
        /// 改
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
       // git clone https://gitee.com/Z06050230Y/egs.git
        //public Result UpLogin([FromBody] EGS_LoginInfo login)
        //{
        //    Result result = new Result();
        //    db.LoginInfo.Update(login);
        //    result.Data = db.SaveChanges();

        //    return result;
        //}


        //[HttpDelete]
        //public bool Delete(Guid ID)
        //{
        //  EGS_LoginInfo? loginInfo = new EGS_LoginInfo();
        //    loginInfo = db.LoginInfo.Where(l => l.ID == ID).FirstOrDefault();
        //    int result = 0;
        //    if (result != null)
        //    {
        //        loginInfo.DelTime = DateTime.Now;
        //        db.LoginInfo.Update(loginInfo);
        //        result = db.SaveChanges();
        //    }
        //    return result > 0 ? true : false;
        //}
    }
}
